﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
using BOHVote.Core;

namespace BOHVote.DAL
{
    public class DictManageProvide
    {
        Database db;

        public DictManageProvide()
        {
            db = DatabaseFactory.CreateDatabase();
        }

        #region <<  查询 字典 >>

        #region << 获取所有字典  >>
        public DataSet getAdict(string dictName,string dictID)
        {
            return getAllDict("1", "A", dictName, dictID, string.Empty);
        }

        public DataSet getDdict(string dictName, string dictID)
        {
            return getAllDict("1", "D", dictName, dictID, string.Empty);
        }

        public string getDicByValue(string dictName, string dictValue)
        {
            string strSQL = "SELECT dict_display FROM sys_dict WHERE 1=1 ";
            if (!String.IsNullOrEmpty(dictName))
                strSQL += " AND dict_Name = '" + dictName + "' ";
            if (!String.IsNullOrEmpty(dictValue))
                strSQL += " AND dict_Value = '" + dictValue + "' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return (string)Utility.ExecuteScalar(db, dbcommand);
        }

        public DataSet getDict(string dictValue, string dictID)
        {
            return getAllDict("1", "", string.Empty, dictID, dictValue);
        }
        #endregion 

        #region <<  获取所有数据字典类型数据  >>
        public DataSet getADataDict(string dictName,string dictID)
        {
            return getAllDict("2", "A", dictName, dictID,string.Empty);
        }

        public DataSet getDDataDict(string dictName, string dictID)
        {
            return getAllDict("2", "D", dictName, dictID, string.Empty);
        }

        public DataSet getDataDict(string dictName, string dictID)
        {
            return getAllDict("2", "", dictName, dictID, string.Empty);
        }
        #endregion

        public DataSet getAllDict(string dictType,string status,string dictName,string dictID,string dictValue)
        {
            string strSQL = "SELECT dict_ID,dict_Value,dict_display,dict_status,dict_name,dict_note FROM sys_dict WHERE 1=1 ";
            if (!string.IsNullOrEmpty(dictType))
            {
                strSQL += " AND dict_type ='" + dictType + "' ";
            }

            if (!string.IsNullOrEmpty(dictValue))
            {
                strSQL += " AND dict_Value = '" + dictValue + "'";
            }

            if (!string.IsNullOrEmpty(dictName))
            {
                strSQL += " AND dict_Name = '" + dictName + "'";
            }

            if (!string.IsNullOrEmpty(status))
            {
                strSQL += " AND dict_status = '" + status + "'";
            }

            if (!string.IsNullOrEmpty(dictID))
            {
                strSQL += " AND dict_id = '" + dictID + "'";
            }
        


            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return Utility.ExecuteDataSet(db, dbcommand);
        }

        #endregion

        #region <<  插入 字典  >>

        public int InsertDataDict(string status, string dictValue, string dictName, string dictDisplay, string dictNote)
        {
            return InsertSysDataDict("2", status, dictValue, dictName, dictDisplay, dictNote);
        }


        public int InsertSysDataDict(string dictType, string status,string dictValue, string dictName,string dictDisplay,string dictNote)
        {
            string strSQL = "INSERT INTO sys_dict(dict_Name,dict_type,dict_value,dict_display,dict_status,dict_note)VALUES('" + dictName + "','" + dictType + "','" + dictValue + "','" + dictDisplay + "','" + status + "','" + dictNote + "')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        #endregion

        #region << 更新 字典 >>
        public int UpdateSysDataDict(string status, string dictValue, string dictName, string dictDisplay, string dictNote,string dictID)
        {
            string strSQL = "UPDATE sys_dict SET dict_status = '" + status + "' ,dict_Name='" + dictName + "',dict_note = '" + dictNote + "' ,dict_Value = '" + dictValue + "' , dict_display ='" + dictDisplay + "' WHERE dict_id = '" + dictID + "' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return Utility.ExecuteNonQuery(db, dbcommand);
        }
        #endregion

        #region << 删除 字典 >>
        public int DeleteDict(string DictID)
        {
            string strSQL = "DELETE FROM sys_dict WHERE dict_id = '" + DictID + "'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return Utility.ExecuteNonQuery(db, dbcommand);
        }
        #endregion
    }
}
